package com.sxbbc.common.core.service.system.redis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.core.entity.jz.ActivitySeckillDetail;
import com.sxbbc.common.core.entity.jz.InfoWx;
import com.sxbbc.common.core.entity.jz.OrderConfigGroup;
import com.sxbbc.common.core.enums.GroupStatusEnums;
import com.sxbbc.common.core.enums.redis.RedisExpiredEnums;
import com.sxbbc.common.core.service.IActivitySeckillDetailService;
import com.sxbbc.common.core.service.IOrderConfigGroupService;
import com.sxbbc.common.core.service.system.redis.listener.OrderListerService;
import com.sxbbc.common.core.utils.RedisUtils;
import com.sxbbc.common.core.wx.WxUtils;

import common.Logger;

/**
 * redis处理监听过期业务逻辑
 *
 * @author arno
 */
@Service
public class RedisExpirationservice {
    private static Logger logger = Logger.getLogger(RedisExpirationservice.class);

    @Autowired
    private IOrderConfigGroupService orderConfigGroupService;
    @Autowired
    private IActivitySeckillDetailService iActivitySeckillDetailService;
    @Autowired
    private RedisSeckillService redisSeckillService;
    @Autowired
    private OrderListerService orderListerService;

    /**
     * 处理最终redis key过期业务
     *
     * @param eventType
     * @param params
     */
    public void handleExpires(RedisExpiredEnums eventType, String params) {
        logger.info("eventType: " + eventType + " ,params:" + params);
        switch (eventType) {
            case ORDER_BUY:
                //处理购买订单过期
                String orderCode = params;
                orderListerService.handleOrderTimeout(orderCode);
                break;
            case WX_WEB_TOKEN:
                //处理微信临时token过期设置
                String token = WxUtils.getInstance().getToken();
                RedisUtils.getInstance().setWxToken(token);
                //保证微信相关的ticket时间同步，解决时间不同步导致数据获取失败的问题
                RedisUtils.getInstance().setWxJsApiTicket(token);
                break;
            case WX_PROGRAM_TOKEN:
                //处理微信小程序临时token过期设置
                InfoWx program = RedisUtils.getInstance().getWxInfo();
                RedisUtils.getInstance().setWxProgramToken(program);
                break;
            case GROUP_ORDER:
                //如果团购拼团过期
                //这里去修改拼团状态 而且退款
                QueryWrapper<OrderConfigGroup> wrapper = new QueryWrapper<>();
                wrapper.eq("groupCode", params);
                OrderConfigGroup config = new OrderConfigGroup();
                config.setStatus(GroupStatusEnums.GROUP_REFUND.getId());
                orderConfigGroupService.update(config, wrapper);
                break;
            case SECKILL_PRO:
                //秒杀商品活动失效
                //ActivitySeckillDetail activitySeckillDetail=iActivitySeckillDetailService.getById(Integer.parseInt(params));
                //activitySeckillDetail.setStatus(StaticUtils.STATUS_NO);
                //iActivitySeckillDetailService.updateById(activitySeckillDetail);
                break;
            case SECKILL_WILL_PRO:
                logger.info("活动开始 将秒杀子项信息放入缓存");
                //活动开始 将秒杀子项信息放入缓存
                List<ActivitySeckillDetail> activitySeckillDetails = iActivitySeckillDetailService.queryValidList(Integer.parseInt(params));
                for (ActivitySeckillDetail a : activitySeckillDetails) {
                    redisSeckillService.setSkDetail(a);
                }
                break;
            case WX_JSAPI_TICKET:
                // 设置微信JS接口的临时票据，放在token同步进行
//                logger.info("设置微信JS接口的临时票据");
//                String token = RedisUtils.getInstance().getWxToken();
//                RedisUtils.getInstance().setWxJsApiTicket(token);
                break;
            default:
                break;
        }
    }
}
